﻿
namespace UsingNonMVVMElements.AttachedBehaviors
{
    using System.Windows;
    using System.Windows.Controls;

    public class WebBrowserAttachedBehavior
    {
        public static DependencyProperty SourcePageProperty =
            DependencyProperty.RegisterAttached("SourcePage", 
                typeof(string), typeof(WebBrowserAttachedBehavior),
                new PropertyMetadata("", OnSourcePagePropertyChanged));

        public static string GetSourcePage(DependencyObject obj)
        {
            return (string)obj.GetValue(SourcePageProperty);
        }

        public static void SetSourcePage(DependencyObject obj, string value)
        {
            obj.SetValue(SourcePageProperty, value);        
        }

        public static void OnSourcePagePropertyChanged(DependencyObject browser, 
            DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != null && browser is WebBrowser)
            {
                WebBrowser webBrowser = (WebBrowser)browser;
                webBrowser.Source = new System.Uri(args.NewValue.ToString());
            }
        }
    }
}
